package com.yunhe.abnormal.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
//import com.yunhe.common.config.EnvironmentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class JPushUtils {

    @Autowired
    private JPushClient jPushClient;
//    @Autowired
//    private EnvironmentConfig environmentConfig;

    public void pushByTag(Map<String, String> param, Map<String, String> extraParam, String tag) {
//        String currentEnvironment = environmentConfig.getCurrentEnvironment();
//        if (!"pro".equals(currentEnvironment)) {
//            tag = currentEnvironment + "_" + tag;
//        }
        PushPayload payload = PushPayload.newBuilder()
                //所有平台的用户
                .setPlatform(Platform.all())
                //registrationId指定用户
                .setAudience(Audience.tag(tag))
                //.setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        //发送ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                //消息体
                                .setAlert(param.get("msg"))
                                //ios提示音
                                .setSound("happy")
                                //附加参数
                                .addExtras(extraParam)
                                .build())
                        //发送android
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                //附加参数
                                .addExtras(extraParam)
                                //消息体
                                .setAlert(param.get("msg"))
                                .build())
                        .build())
                //指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                //自定义信息
                .setMessage(Message.newBuilder().setMsgContent(param.get("msg")).addExtras(extraParam).build())
                .build();
        try {
            payload.resetOptionsTimeToLive(15);
            jPushClient.sendPush(payload);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
